package com.gs.SynPatrolRep.fragment;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.adapter.InsightAdapter;
import com.gs.SynPatrolRep.apiresult.ApiResultData;
import com.gs.SynPatrolRep.constant.EventCode;
import com.gs.SynPatrolRep.control.GridLayoutManager;
import com.gs.SynPatrolRep.control.InsightView;
import com.gs.SynPatrolRep.db.bean.Event;
import com.gs.SynPatrolRep.db.dto.InsightDto;
import com.gs.SynPatrolRep.db.dto.PalBillStatusRateDto;
import com.gs.SynPatrolRep.db.query.BaseQuery;
import com.gs.SynPatrolRep.fragment.base.BaseListFragment;
import com.gs.SynPatrolRep.presenter.InsightPresenter;
import com.gs.SynPatrolRep.view.IInsightView;

import org.greenrobot.eventbus.EventBus;

public class InsightFragment extends BaseListFragment<IInsightView, InsightPresenter, PalBillStatusRateDto, BaseQuery> implements IInsightView {

    private InsightView itemBeInspected,itemInspected,itemTimeout;
    private boolean isShow = true, isThread = true;
    private static Thread thread;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_insight;
    }

    @Override
    protected InsightPresenter createPresenter() {
        return new InsightPresenter();
    }

    @Override
    protected BaseQuery createQuery() {
        return new BaseQuery();
    }

    @Override
    protected void initView() {
        smartRefreshLayout.setEnableLoadMore(false);
        itemBeInspected = fd(R.id.item_beinspected);
        itemBeInspected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new Event(EventCode.EVENT_JUMP,0));
            }
        });
        itemInspected = fd(R.id.item_inspected);
        itemInspected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new Event(EventCode.EVENT_JUMP,6));
            }
        });
        itemTimeout = fd(R.id.item_timeout);
    }

    @Override
    protected void loadData() {
        query.init();
        mPresenter.getData();
    }

    @Override
    protected void search() {
        if (thread != null) thread.interrupt();
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (isThread) {
                        if (isShow) {
                            query.init();
                            mPresenter.getData();
                        }
                        Thread.sleep(10 * 1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    @Override
    public void filter() {

    }

    @Override
    public void onGetDataSuccess(ApiResultData<InsightDto> apiResult) {
        refreshSmartRefreshLayout();
        if (apiResult.getStatus() == 2000) {
            InsightDto data = apiResult.getData();
            itemBeInspected.setNum(data.getAwaitBillPalCount());
            itemInspected.setNum(data.getAwaitBillPalApproverCount());
            itemTimeout.setNum(data.getOtBillPalCount());
        } else if (apiResult.getStatus() == 4001) { // token过期不再继续循环请求
            isShow = false;
        }
    }

    @Override
    public void onGetDataFails(Exception e) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (thread != null) {
            thread.interrupt();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isShow = !hidden;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            isShow = true;
        }
    }
}
